package com.ls.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ls.config.satoken.SaTokenUtilPy;
import com.ls.controller.holiday.model.EditHolidayModel;
import com.ls.controller.label.model.AddLabelModel;
import com.ls.controller.label.model.EditLabelModel;
import com.ls.controller.label.model.QueryLabelModel;
import com.ls.entity.*;
import com.ls.mapper.LabelMapper;
import com.ls.service.ICompanyService;
import com.ls.service.ICustomerLabelService;
import com.ls.service.ILabelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ls.service.ILinkmanService;
import com.ls.utils.ResultData;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

import static com.ls.constant.SystemConstants.VALID_FALSE;
import static com.ls.constant.SystemConstants.VALID_TRUE;

/**
 * <p>
 * 标签表，与customer关联 服务实现类
 * </p>
 *
 * @author pengyu
 * @since 2024-11-20
 */
@RequiredArgsConstructor
@Service
public class LabelServiceImpl extends ServiceImpl<LabelMapper, Label> implements ILabelService {

//    private final ICustomerLabelService customerLabelService;
    private final ILinkmanService linkmanService;

    @Override
    public ResultData pageList(QueryLabelModel model) {
        Long customerId=null;
        if (model.getCustomerId()==null){
            Customer customer = SaTokenUtilPy.getCustomer();
            customerId=customer.getCustomerId();
        }else{
            customerId=model.getCustomerId();
        }
        //1 查询出标签列表并包含成员数量
        List<Label> labelList = baseMapper.selectLabelListWithMemberCount(
                customerId, 
                StringUtils.isNotBlank(model.getLabelName()) ? model.getLabelName() : null, 
                model.getLabelType());
        return ResultData.successData(labelList);
    }

    @Override
    public ResultData add(AddLabelModel model) {
        Customer customer = SaTokenUtilPy.getCustomer();
        Long customerId = customer.getCustomerId();
        //根据用户id查询用户企业
        Label label1 = getOne(new LambdaQueryWrapper<Label>()
                .eq(Label::getCustomerId, customerId)
                .eq(Label::getValidFlag, VALID_TRUE)
                .eq(Label::getLabelType, model.getLabelType())
                .eq(Label::getLabelName, model.getLabelName()));
        if (label1 != null) {
            return ResultData.failure("当前用户已存在该标签");
        }
        Label label = new Label();
        BeanUtils.copyProperties(model, label);
        label.setCustomerId(customerId);
        label.setCreatorId(customerId);
        label.setUpdaterId(customerId);
        label.setUpdateTime(LocalDateTime.now());
        label.setCreateTime(LocalDateTime.now());
        label.setValidFlag(VALID_TRUE);
        boolean flag = save(label);
        if (flag) {
            return ResultData.success();
        } else {
            return ResultData.failure("内部服务器错误");
        }
    }
    @Override
    public ResultData edit(EditLabelModel model) {
        Customer customer = SaTokenUtilPy.getCustomer();
        Long customerId = customer.getCustomerId();
        //判断当前用户是否存在重复的标签
        Label label1 = getOne(new LambdaQueryWrapper<Label>()
                .eq(Label::getCustomerId, customerId)
                .eq(Label::getValidFlag, VALID_TRUE)
                .eq(Label::getLabelName, model.getLabelName())
                .eq(Label::getLabelType, model.getLabelType())
                .ne(Label::getLabelId, model.getLabelId())
        );
        if (label1 != null) {
            return ResultData.failure("当前用户已存在该标签！");
        }
        Label label = getById(model.getLabelId());
        BeanUtils.copyProperties(model, label);
        label.setUpdateTime(LocalDateTime.now());
        label.setUpdaterId(customerId);
        Boolean flag = saveOrUpdate(label);
        if (flag) {
            return ResultData.successMsg("更新成功");
        } else {
            return ResultData.failure("更新失败");
        }
    }

    @Override
    public ResultData del(Integer labelId) {
        Label label = getById(labelId);
        if (label == null) {
            return ResultData.failure("标签名称不存在，无需删除");
        }
        //判断是否被使用
        int count = linkmanService.count(new LambdaQueryWrapper<Linkman>().eq(Linkman::getLabelId, labelId));
        if (count > 0) {
            return ResultData.failure("标签名称已经被引用，不能删除");
        }
        label.setValidFlag(VALID_FALSE);
        boolean flag = saveOrUpdate(label);
        if (flag) {
            return ResultData.success();
        } else {
            return ResultData.failure("内部服务器错误");
        }
    }
}
